package com.itheima.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.AddressBook;
import com.itheima.mapper.AddressBookMapper;
import com.itheima.mapper.MpAddressBookMapper;
import com.itheima.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author itheima
 * @since 2022-05-02
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public AddressBook addAddressBook(AddressBook addressBook) {
        // Objects.isNull(addressBook)  =? addressBook == null
        if (Objects.isNull(addressBook)) {
            return null;
        }

        // 使用 hutool 工具类，生成主键
        String id = new Snowflake().nextIdStr();
        addressBook.setId(id);
        addressBook.setUserId("123123");

        boolean result = addressBookMapper.insert(addressBook) > 0;
        if (!result) {
            return null;
        }

        return addressBook;
    }

    @Override
    public Boolean modifyAddressBook(AddressBook addressBook) {
        if (Objects.isNull(addressBook)) {
            return false;
        }

        return addressBookMapper.updateById(addressBook) > 0;
    }

    @Override
    public Boolean removeAddressBookInIds(List<String> idList) {
        return addressBookMapper.deleteByBatchId(idList) == idList.size();
    }
}
